package com.situ.edu.controller;

import com.situ.edu.pojo.entity.Course;
import com.situ.edu.pojo.entity.Xi;
import com.situ.edu.pojo.query.XiQuery;
import com.situ.edu.pojo.vo.CourseCountVO;
import com.situ.edu.service.IXiCourseService;
import com.situ.edu.service.IXiService;
import com.situ.edu.service.impl.XiCourseServiceImpl;
import com.situ.edu.service.impl.XiServiceImpl;
import com.situ.edu.util.MyBeanUtils;
import com.situ.edu.util.PageInfo;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/xiCourse")
public class XiCourseController extends BaseController{
    private IXiCourseService xiCourseService = new XiCourseServiceImpl();
    private IXiService xiService = new XiServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByCourse"://查询课程
                selectByCourse(req,resp);
                break;
            case "selectAll":
                selectAll(req,resp);
                break;
            case "deleteById"://删除单个课程
                deleteById(req,resp);
                break;
            case "deleteAll"://批量删除
                deleteAll(req,resp);
                break;
            case "add"://添加课程名，学分，所在系
                add(req,resp);
                break;
            case "selectById"://查询id,回显数据
                selectById(req,resp);
                break;
            case "update"://编辑课程
                update(req,resp);
                break;
            case "selectCourseCount"://echarts图
                selectCourseCount(req,resp);
                break;
            case "exportExcel": //导出Excel
                exportExcel(req,resp);
                break;
            case "importExcel":
                importExcel(req,resp);
                break;

        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.selectAll");
        List<Course> list = xiCourseService.selectAll();
        toJSON(resp, Result.ok(list));
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.importExcel");
        xiCourseService.importExcel(req);
        toJSON(resp,Result.ok("上传成功"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.exportExcel");
        xiCourseService.exportExcel(resp);
    }


    private void selectCourseCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.selectCourseCount");
        List<CourseCountVO> list = xiCourseService.selectCourseCount();
        toJSON(resp,Result.ok(list));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Course course = MyBeanUtils.copyToBean(req,Course.class);
        xiCourseService.update(course);

        toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.selectById");
        String id = req.getParameter("id");
        Course course = xiCourseService.selectById(Integer.parseInt(id));
        List<Xi> list = xiService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("course",course);
        map.put("list",list);

        toJSON(resp,Result.ok(map));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.add");

        Course course = MyBeanUtils.copyToBean(req, Course.class);
        xiCourseService.add(course);

        toJSON(resp,Result.ok("添加成功"));

    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        xiCourseService.deleteAll(ids);
        toJSON(resp,Result.ok("删除成功"));

    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.deleteById");
        String id = req.getParameter("id");
        xiCourseService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByCourse(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("XiCourseController.selectByCourse");
        XiQuery xiQuery = MyBeanUtils.copyToBean(req,XiQuery.class);
        PageInfo pageInfo = xiCourseService.selectByCourse(xiQuery);
        toJSON(resp,pageInfo);
    }
}
